package com.zhouyusen.controller;

import com.zhouyusen.entity.User;
import com.zhouyusen.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.print.attribute.standard.PrinterURI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Zhou YuSen
 * @date 2020/7/9 17:57
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 通过Id删除User
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public String deleteById(@PathVariable("id") Long id) {
        int deleteById = userService.deleteById(id);
        if (deleteById == 1) {
            return "删除成功";
        }
        return "删除失败";
    }

    /**
     * 添加User
     * @param record
     * @return
     */
    @PostMapping("/insertuser")
    public String insert(User record) {
        int insert = userService.insert(record);
        if (insert == 1) {
            return "添加成功";
        }
        return "添加失败";
    }

    /**
     * 通过不同条件添加User，动态添加
     * @param record
     * @return
     */
    @PostMapping("/insertusersel")
    public String insertSelective(User record) {
        int insertSelective = userService.insertSelective(record);
        if (insertSelective == 1) {
            return "添加成功";
        }
        return "添加失败";
    }

    /**
     * 通过Id查询User
     * @param id
     * @return
     */
    @GetMapping("/getbyid/{id}")
    public Map selectById(@PathVariable("id") Long id) {
        User user = userService.selectById(id);
        HashMap<String, Object> map = new HashMap<>();
        if (user != null) {
            map.put("User数据",user);
            return map;
        }
        map.put("错误信息","未查询到内容！");
        return map;
    }

    /**
     * 查询所有User
     * @return
     */
    @GetMapping("/getAll")
    public List<User> selectAll() {
        List<User> list = userService.selectAll();
        return list;
    }

    /**
     * 通过Id更新 动态更新
     * @param record
     * @return
     */
    @PutMapping("/updatebyuser")
    public String updateByPrimaryKeySelective(User record) {
        int updateByPrimaryKeySelective = userService.updateByPrimaryKeySelective(record);
        if (updateByPrimaryKeySelective == 1) {
            return "更新成功";
        }
        return "更新失败";
    }

    /**
     * 通过Id更新
     * @param record
     * @return
     */
    @PutMapping("/update")
    public String updateById(User record) {
        int update = userService.updateById(record);
        if (update == 1) {
            return "更新成功";
        }
        return "更新失败";
    }
}
